package com.mida.bms.report.service.impl;

import com.mida.bms.report.entity.DocStructReport;
import com.mida.bms.report.innerdto.ReportFlowInfo;
import com.mida.bms.report.mapper.DocStructReportMapper;
import com.mida.bms.report.service.SrvDocReportService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.concurrent.TimeUnit;

/**
 * 暂时用来放公用的，与DocReportService有关的方法，之后会变成真正的service,原DocReportService升级为 process
 */
@Service
@Slf4j
@Transactional
public class SrvDocReportServiceImpl implements SrvDocReportService {

    @Autowired
    private StringRedisTemplate redisTemplate;

    // 报告存储信息
    @Autowired
    private DocStructReportMapper docStructReportMapper;

    @Override
    public void updateStructReport(DocStructReport docStructReport){
        ReportFlowInfo reportFlowInfo = new ReportFlowInfo();
        BeanUtils.copyProperties(docStructReport,reportFlowInfo);

        cacheStructFlowInfo(reportFlowInfo);
        docStructReportMapper.updateByPrimaryKey(docStructReport);
    }

    @Override
    public void updateStructReportSelective(DocStructReport docStructReport){
        ReportFlowInfo reportFlowInfo = new ReportFlowInfo();
        BeanUtils.copyProperties(docStructReport,reportFlowInfo);

        cacheStructFlowInfo(reportFlowInfo);
        docStructReportMapper.updateByPrimaryKeySelective(docStructReport);
    }

    @Override
    public void insertStructReport(DocStructReport report){
        ReportFlowInfo reportFlowInfo = new ReportFlowInfo();
        BeanUtils.copyProperties(report,reportFlowInfo);

        cacheStructFlowInfo(reportFlowInfo);
        docStructReportMapper.insert(report);
    }

    @Override
    public void cacheStructFlowInfo(ReportFlowInfo reportFlowInfo){
        String keyName = "reportFlowInfo" + reportFlowInfo.getReportSoid();
        redisTemplate.opsForHash().put(keyName,"status",String.valueOf(reportFlowInfo.getStatus()));
        redisTemplate.opsForHash().put(keyName,"reportProcessStatus",String.valueOf(reportFlowInfo.getReportProcessStatus()));
        redisTemplate.opsForHash().put(keyName,"firstTrialDoctor",String.valueOf(reportFlowInfo.getFirstTrialDoctor()));
        redisTemplate.opsForHash().put(keyName,"firstWriteDoctor",String.valueOf(reportFlowInfo.getFirstWriteDoctor()));
        redisTemplate.expire(keyName, 1, TimeUnit.DAYS);
        // FIXME: yutao 2021/8/8 对应的中文名，加一下,下面两个，先写死。以后再说
        // TODO: yutao 2021/8/8 李建补一下，从对应的status,reportProcessStatus中取到相关的值
        redisTemplate.opsForHash().put(keyName,"statusName",String.valueOf(reportFlowInfo.getStatusName()));
        redisTemplate.opsForHash().put(keyName,"reportStatus",String.valueOf(reportFlowInfo.getReportStatus()));
    }

}
